<?php
namespace Admin\Controller;
use Common\Model\CommonModel;
use Think\Controller;
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/11/7
 * Time: 15:48
 */

 class NewscateController extends BaseController{

     /**
      *
      */
     public function index(){
        $newscateModel=M('Newscate');
        //var_dump($newscateModel);exit;
         $condition['name'] = array('like',"%".$_GET['name']."%");
         unset($_SESSION['like']['newscate']);
         $_SESSION['like']['newscate']=$_GET['name'];

         if(!empty($_GET['id'])){
            $condition['pid']=$_GET['id'];
         }else if($_GET['pid']){
             $newscateModel=D('Newscate');
             $cates=$newscateModel->finded('id='.$_GET['pid']);
             $condition['pid']=$cates['pid'];
         }else{
             $condition['pid']=0;
         }
        $count = $newscateModel->where($condition)->count();
        $page= new \Think\Page($count,5);
        $page->setConfig('next','下一页');
        $page->setConfig('prev','上一页');
        //显示还有多少条
        $page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $page->show();
        $list=$newscateModel->where($condition)->order(array('create'=>'desc'))->limit($page->firstRow.','.$page->listRows)->select();
        $list=CommonModel::status($list);
       // var_dump($list);exit;
       /*  if(empty($list)){
             $this->
         }*/
        $this->assign('page',$show);
        $this->assign('list',$list);
        $this->display();

    }

     public function create(){
        $newscateModel=D('Newscate');
        if(!empty($_GET)){
            $pid=$_GET['id'];
            $this->assign('pid',$pid);
        }else{
            $list=$newscateModel->getpid();
            $this->assign('cates',$list);
            $this->assign('pid',0);
        }

         $this->display();
     }

     public function doCreate(){
         $newscateModel=D('Newscate');
         $data['pid']=$_POST['pid'];
         $data['name']=$_POST['name'];
         $path=$newscateModel->findPath('id='.$data['pid']);
         $data['path']=$path.','.$data['pid'];

         $data['create']=time();
         //  echo $_POST['name'];exit;
         // echo $data['name'];exit;

         $res=$newscateModel->create($data);
         if($res){
             $this->ajaxreturn(1);
         }else{
             $this->ajaxreturn(2);
         }
     }
     //修改页面
     public function update(){
         $newscateModel=D('Newscate');
         //$newscateModel=new PriceModel();
         $da['id']=$_GET['id'];
         //找到原数据
         $list=$newscateModel->finded($da);
         $this->assign('list',$list);
         $this->display();
     }
     //执行修改
     public function doUpdate(){
         $newscateModel=D('Newscate');
         $data['name']=$_POST['name'];
         $id=$_POST['id'];
         $where='id='.$id;
         // echo $id;exit;
         $list=$newscateModel->finded($where);
         /* echo(json_encode($list));
          exit;*/
         $data['update']=time();
        // echo 2; exit;
         //过滤重复内容
         $data=CommonModel::filter($data,$list);
         // echo json_encode($data);
         // exit;
         $res=$newscateModel->update($where,$data);
         if($res){
             $this->ajaxReturn(1);
         }else{
             $this->ajaxReturn(2);
         }
     }

     public function del(){
         $newscateModel=D('Newscate');
         $id=$_GET['id'];
        // $pid=$newscateModel->finded('pid='.$id);
         $list=$newscateModel->finded('pid='.$id);
         if(empty($list)){
             $res= $newscateModel->deleted('id='.$id);
             if($res) {
                 $this->success('删除成功');
             }else{
                 $this->error('删除失败');
             }
         }else{
             $this->error('存在子类，不能删除!');
         }
    }
 }